$(document).ready(function(){
    $('.menu-list li').click(function(e){
        if($(e.target).hasClass('unuseable')){
            return;
        }

        $(e.target).addClass('active').siblings().removeClass('active');
    });

    $('#reset').click(function(e){
        $('#job-title,#job-city,#job-salary,#job-exp,#job-description,#job-industry,#job-history-comp').val('');
        $('.match-result-container').addClass('hidden');
    });

    $('#match').click(function(e){
        var exp = $('#job-exp').val();
        var edu = $('#job-edu').val();
        var title = $('#job-title').val();
        var location = $('#job-city').val();
        var salary = $('#job-salary').val();
        var industry = $('#job-industry').val();
        var company = $('#job-history-comp').val();
        var description = $('#job-description').val();
        var brandUniversity = $('#brandUniversity').is(':checked');

        $('.match-result-container').addClass('hidden');
        $('#match').text('正在匹配...');

        $.ajax({
            'url':'/resum_job_match/match.do',
            'dataType': 'json',
            'data':{
                'exp':exp,
                'edu':edu,
                'title':title,
                'salary':salary,
                'company':company,
                'location':location,
                'industry':industry,
                'description':description,
                'brandUniversity':brandUniversity
            },
            'success':function(resp){
                if(resp.success){
                    var htmlstr = "";
                    for(var i = 0;i < resp.matched.length;i++){
                        htmlstr += '<tr>';
                        htmlstr += '<th scope = "row">' + i + '</th>';
                        htmlstr += '<td><a target = "_blank" href = "/detail.html?id=' + resp.matched[i]['id'] + '">' + resp.matched[i]['wanted_job']['title'] + '</a></td>';
                        htmlstr += '<td>' + resp.matched[i]['personal_info']['edu'] + '</td>';
                        htmlstr += '<td>' + resp.matched[i]['personal_info']['age'] + '</td>';
                        htmlstr += '<td>' + resp.matched[i]['personal_info']['exp'] + '</td>';
                        htmlstr += '<td>' + resp.matched[i]['wanted_job']['salary'] + '</td>';
                        htmlstr += '<td>' + resp.matched[i]['wanted_job']['location'] + '</td>';
                        htmlstr += '<td>' + resp.matched[i]['platform'] + '</td>';
                        htmlstr += '<td>' + resp.matched[i]['wanted_job']['current'] + '</td>';
                        htmlstr += '</tr>';
                    }

                    $('.match-result-container tbody').html(htmlstr);
                    $('.match-result-container').removeClass('hidden');
                    $('#match').text('开始匹配');
                } else {
                    $('#match').text('开始匹配');
                    $('.match-result-container').addClass('hidden');
                    alert(resp.msg);
                }
            }
        });
    });

    function display_industry_labels(labels){
        var htmlstr = '';

        for(var i = 0;i < labels.length;i++){
            htmlstr += '<button type = "button" class = "btn btn-outline-danger" style = "border-radius:25px;margin-right:4px;margin-bottom:4px;">' + labels[i] + '</button>'
        }

        $('.industry-labels-container').html(htmlstr);
    }

    var industry_labels = [];
    var selected_industry_labels = [];

    $.ajax({
        'url':'/industry_labels.do',
        'success':function(resp){
            if(resp.success){
                industry_labels = resp.data;
                display_industry_labels(industry_labels);
            }
        }
    });

    $('#label-dig-input').on('keyup',function(e){
        var query = $('#label-dig-input').val();
        var result = [];

        for(var i = 0;i < industry_labels.length;i++){
            if(industry_labels[i].indexOf(query) != -1){
                result.push(industry_labels[i]);
            }
        }

        display_industry_labels(result);
    });

//    $('#job-industry').click(function(){
//        $('.industry-label-modal').removeClass('hidden');
//    });

    $('.industry-label-modal .fa-close').click(function(){
        $('.industry-label-modal').addClass('hidden');

        var labels = [];
        var selected = $('.industry-selected-labels-container .btn');

        if(selected.length){
            for(var i = 0;i < selected.length;i++){
                labels.push(selected.eq(i).text());
            }
        }

        $('#job-industry').val(labels.join(','));
    });

    $('.industry-labels-container').delegate('.btn','click',function(e){
        $('.industry-selected-labels-container').append('<button type = "button" class = "pr btn btn-danger" style = "border-radius:25px;margin-right:7px;">'+$(e.target).text()+'<a class = "fa fa-close"></a></button>');
    });

    $('.industry-selected-labels-container').delegate('.fa-close','click',function(e){
        $(e.target).closest('.btn').remove();
    });
});